﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Processing;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WisDom.SmartTMS.Interface;

namespace WisDom.SmartTMS.Controllers
{
    [ApiController]   //所有API控制器都必须有这个注解
    [Route("api/[controller]/[action]")]
    public class VehicleController:Controller
    {
        public readonly IVehicleService _vehicleService;
        public VehicleController(IVehicleService vehicleService)
        {
            _vehicleService = vehicleService;
        }



        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpLoad(IFormFile file)
        {
            string rootdir = AppContext.BaseDirectory.Split(@"\bin\")[0];
            string fname = DateTime.Now.ToString("yyyyMMddHHmmssffff") + System.IO.Path.GetExtension(file.FileName);
            var path = rootdir + @"\static\images\" + fname;
            using (System.IO.FileStream fs = System.IO.File.Create(path))
            {
                file.CopyTo(fs);
                fs.Flush();//清空文件流
            }
            //找到图片
            Image image = Image.Load(path);
            //缩小图片
            image.Mutate(m => m.Resize(image.Width / 2, image.Height / 2));
            //生成缩略图
            string rootdirthumbnail = AppContext.BaseDirectory.Split(@"\bin\")[0];
            string fnamethumbnail = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "Thumbanilimg" + System.IO.Path.GetExtension(file.FileName);
            string paththumbnail = rootdirthumbnail + @"\static\images\" + fnamethumbnail;
            //保存缩略图
            image.Save(paththumbnail);
            return Ok(new { newFileName = "http://localhost:8031/" + fname });//将能访问新文件网址回传给前端
        }
    }
}
